package org.zachary.commonservice.strategy.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zachary.commonmodule.entity.MqTransmissionJson;
import org.zachary.commonservice.entity.CreditScoreStrategy;
import org.zachary.commonservice.entity.ServiceType;
import org.zachary.commonservice.mapper.CreditScoreStrategyMapper;
import org.zachary.commonservice.strategy.CreditStrategy;
import org.zachary.commonservice.strategy.cache.StrategyCache;

@Component
public class HouseRentalStrategy implements CreditStrategy {
    @Autowired
    private CreditScoreStrategyMapper strategyMapper;
    @Autowired
    private StrategyCache cache;

    @Override
    public ServiceType getServiceType() {
        return ServiceType.LeaseOfHouses;
    }

    @Override
    public int calculate(MqTransmissionJson message, int currentScore) {
        System.out.println("进入HouseRental策略");
        // 1. 加载配置（基于用户当前信用分）
        CreditScoreStrategy config = loadConfig(message.getServiceType(), currentScore);
        if (config == null) {
            throw new IllegalStateException("未找到匹配的信用策略配置");
        }
        System.out.println("策略匹配完成: " + config);

        System.out.println("HouseRentalStrategy - 消息详情:");
        System.out.println("  isOverdue: " + message.getIsOverdue());
        System.out.println("  finishTime: " + message.getFinishTime());
        System.out.println("  dueTime: " + message.getDueTime());
        System.out.println("  overduePeriod: " + message.getOverduePeriod());
        System.out.println("  amount: " + message.getAmount());
        System.out.println("  desc: " + message.getDesc());
        System.out.println("  isPaid: " + message.getIsPaid());

        // 检查是否已支付（finishTime不为空表示已支付）
        boolean hasPaid = message.getFinishTime() != null;

        // 详细记录消息处理逻辑
        System.out.println("HouseRentalStrategy - 处理逻辑分析:");
        System.out.println("  isOverdue标志: " + message.getIsOverdue());
        System.out.println("  是否已支付(finishTime非空): " + hasPaid);

        if (message.getIsOverdue()) {
            if (hasPaid) {
                // 逾期后支付 - 根据逾期期数扣分
                int minusScore = config.getPenaltyByPeriod(message.getOverduePeriod());
                double baseAmount = config.getAmount() > 0 ? config.getAmount() : 1.0;
                int amountRatio = (int) (message.getAmount() / baseAmount);
                // 根据金额比例计算总扣分：minusScore * 金额比例
                int totalPenalty = minusScore * Math.max(1, amountRatio);
                System.out.println("租房逾期后支付扣分分值: " + (-totalPenalty));
                System.out.println("  逾期期数: " + message.getOverduePeriod());
                System.out.println("  对应minusScore: " + minusScore);
                System.out.println("  金额比例: " + amountRatio);
                System.out.println("  总扣分: " + totalPenalty);
                return -totalPenalty;
            } else {
                // 逾期未支付 - 只发消息不扣分
                System.out.println("租房逾期未支付 - 只发消息不扣分");
                System.out.println("  逾期期数: " + message.getOverduePeriod());
                return 0; // 不扣分
            }
        } else {
            // 守约加分
            double baseAmount = config.getAmount() > 0 ? config.getAmount() : 1.0;
            int amountRatio = (int) (message.getAmount() / baseAmount);
            int finalScore = config.getAddScore() + amountRatio;
            System.out.println("租房守约加分分值: " + finalScore);
            System.out.println("  基础加分: " + config.getAddScore());
            System.out.println("  金额比例加分: " + amountRatio);
            return finalScore;
        }
    }

    private CreditScoreStrategy loadConfig(Integer serviceTypeCode, int userScore) {
        ServiceType serviceType = ServiceType.values()[serviceTypeCode];
        return cache.getStrategy(serviceType, userScore);
    }
}